const pool = require('./db');

const loginDB = (name, password) => {
    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, connection) {
            if (err) throw err; // 错误
            connection.query(`SELECT * FROM user where name = ${connection.escape(name)} and password = ${connection.escape(password)}`,
                function (error, results) {
                    // 首先释放资源
                    connection.release();
                    // 处理错误
                    if (error) {
                        reject(error);
                    } else {
                        if (!!results.length) {
                            console.log(results)
                            resolve(results[0])
                        } else {
                            resolve(null)
                        }
                    }
                });
        })
    });
}

exports.loginDB = loginDB;